import java.io.*;
import java.util.Scanner;

public class Demo18 {
	public static void main1(String[] args) {
		System.out.println("请输入正确的目录：");
		Scanner scanner =new Scanner(System.in);
		String rootpath=scanner.nextLine();
		//先判断这个是不是一个目录，再进行下面的操作
		File file=new File(rootpath);
		if(!file.isDirectory()) {
			System.out.println("输入的目录有误");
			return ;
		}
		System.out.println("请输入文件中想要查找的关键词：");
		String word=scanner.nextLine();
		File[] files=file.listFiles();
		for (File file1:files) {
			//分开处理
			//如果是一个文件就看是否包含了关键词
			if(file1.isFile()) {
				checkDeleteFile(file1,word);
			} else {
				//扫描文件
				scanDirectory(file1,word);
			}
		}
	}

	private static void scanDirectory(File file1, String word) {
		if(file1==null) {
			return ;
		}
		File[] files=file1.listFiles();
		if(file1.isDirectory()) {
			for(File file:files) {
				System.out.println("当前扫描的路径为："+file.getPath());
				if(file.isDirectory()) {
					scanDirectory(file,word);
				} else {
					checkDeleteFile(file,word);
				}
			}
		}
	}
	private static void checkDeleteFile(File file1, String word) {
		if(!file1.getName().contains(word)) {
			return ;
		}
		//接下来就是该真正的删除了
		System.out.println("当前文件："+file1.getAbsolutePath()+"是否要删除:(Y/n)");
		Scanner scanner =new Scanner(System.in);
		String choice=scanner.nextLine();
		if(choice.equals("y")||choice.equals("Y")) {
			file1.delete();
			System.out.println("删除成功");
		} else {
			System.out.println("取消删除");
		}
	}

	public static void main(String[] args) throws IOException {
		try (OutputStream os = new FileOutputStream("d:/test.txt")) {
			//这里还是将一个字节流转化成一个字符流
			try (OutputStreamWriter osWriter = new OutputStreamWriter(os, "UTF-8")) {
				//这里再包裹一层是，利用prinln的特性将字符串转化成字符写入到OutputStreamWriter中
				try (PrintWriter writer = new PrintWriter(osWriter)) {
					while(!writer.checkError()) {
						writer.print("你好世界");
					}
					writer.flush();
				}
			}
		}
	}
}
